<?php
// 应用公共文件

/**
 * 获取系统路径
 * @param $name
 * @return string
 */
if (!function_exists('com_get_path')) {
    function com_get_path($name)
    {
        $root_path = app()->getRootPath();
        switch ($name) {
            case 'template':
                $path = $root_path . 'template/';
                break;

            case 'public':
                $path = $root_path . 'public/';
                break;

            case 'data':
                $path = $root_path . 'data/';
                break;

            case 'addons':
                $path = $root_path . 'addons/';
                break;

            default:
                $path = $root_path;
                break;
        }
        return $path;
    }
}

/**
 * 生成随机字符串
 * @param int $length 字符串长度
 * @param string $chars 随机字符串范围
 * @return string
 */
if (!function_exists('com_random')) {
    function com_random($length = 16, $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
    {
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
}

/**
 * 获取系统配置
 * @param string $name 配置健名 all-全部
 * @param string $scope 配置模块
 * @return array|string
 */
if (!function_exists('com_get_config')) {
    function com_get_config($name, $scope)
    {
        $ConfigModel = new tmcore\services\ConfigService();
        return $ConfigModel->getConfig($name, $scope);
    }
}

/**
 * 获取文件路径
 * @param string $file_path
 * @param string $type
 * @return string
 */
if (!function_exists('get_file_url')) {
    function get_file_url($file_path, $model = '')
    {
        if (!$file_path) return '';
        if (strpos($file_path, 'https:') !== false || strpos($file_path, 'http:') !== false) {
            return $file_path;
        } else {
            $model = $model?'/' . $model:'';
            return request()->domain() . $model . '/' . $file_path;
        }
    }
}

/**
 * 密码加密
 * @param string $plaintext
 * @param string $salt
 * @return string
 */
if (!function_exists('password_encrypt')) {
    function password_encrypt(string $plaintext, string $salt) : string
    {
        return md5($salt . md5($plaintext . $salt));
    }
}

/**
 * 数组分页
 * @param array $data 数据
 * @param integer $count 每页条数
 * @param integer $page 分页页数
 * @param integer $order 顺序 0-正序 1-倒叙
 * @return array
 */
if (!function_exists('page_array')) {
    function page_array($data,$count,$page,$order = 0){
        // 判断当前页面是否为空 如果为空就表示为第一页面
        $page = (empty($page))?'1':$page;
        // 计算每次分页的开始位置
        $start = ($page-1)*$count;
        if($order == 1) $data = array_reverse($data);
        $totals = count($data);
        // 计算总页面数
        $countPage = ceil($totals/$count);
        $pageData = array_slice($data,$start,$count);
        return [
            'list' => $pageData,
            'page_limit' => $count,
            'page_num' => $page,
            'page_total' => count($data)
        ];
    }
}

/**
 * 多级线性结构排序
 * 转换前：
 * [{"id":1,"pid":0,"name":"a"},{"id":2,"pid":0,"name":"b"},{"id":3,"pid":1,"name":"c"},
 * {"id":4,"pid":2,"name":"d"},{"id":5,"pid":4,"name":"e"},{"id":6,"pid":5,"name":"f"},
 * {"id":7,"pid":3,"name":"g"}]
 * 转换后：
 * [{"id":1,"pid":0,"name":"a","level":1},{"id":3,"pid":1,"name":"c","level":2},{"id":7,"pid":3,"name":"g","level":3},
 * {"id":2,"pid":0,"name":"b","level":1},{"id":4,"pid":2,"name":"d","level":2},{"id":5,"pid":4,"name":"e","level":3},
 * {"id":6,"pid":5,"name":"f","level":4}]
 * @param array $data 线性结构数组
 * @param string $symbol 名称前面加符号
 * @param string $name 名称
 * @param string $id_name 数组id名
 * @param string $parent_id_name 数组祖先id名
 * @param int $level 此值请勿给参数
 * @param int $parent_id 此值请勿给参数
 * @return array
 */
if (!function_exists('linear_to_tree')) {
    function linear_to_tree($data, $sub_key_name = 'sub', $id_name = 'id', $parent_id_name = 'pid', $parent_id = 0)
    {
        $tree = [];
        foreach ($data as $row) {
            if ($row[$parent_id_name] == $parent_id) {
                $temp = $row;
                $child = linear_to_tree($data, $sub_key_name, $id_name, $parent_id_name, $row[$id_name]);
                if ($child) {
                    $temp[$sub_key_name] = $child;
                }
                $tree[] = $temp;
            }
        }
        return $tree;
    }
}

/**
 * 随机生成token值
 * @param string $extra
 * @return string
 */
if (!function_exists('create_token')) {
    function create_token(string $extra = '') : string
    {
        return md5($extra . time());
    }
}

/**
 * 打印日志
 * @param string $path
 * @param string $data
 * @return int|false
 */
if (!function_exists('printLog')) {
    function printLog($path, $data)
    {
        return file_put_contents(app()->getRootPath() . $path,json_encode($data, 320) . PHP_EOL,FILE_APPEND);
    }
}

/**
 * 执行插件事件
 * @param string $event 事件名称
 * @param string $identify 插件标识
 * @param array|null $params 传入参数
 * @param bool $once 是否只返回一个结果
 * @return string
 */
if (!function_exists('addon_event')) {
    function addon_event($event, $identify = '', $params = null, $once = false)
    {
        $result = think\facade\Event::trigger( $identify . '_' . $event, $params, $once);
        return join('', $result);
    }
}

/**
 * 执行插件队列
 * @param string $identify 插件标识
 * @param string $job_method_name 插件方法名称
 * @param array $job_data 插件数据
 * @return void
 */
if (!function_exists('addon_job')) {
    function addon_job($identify, $job_method_name, $job_data)
    {
        return call_user_func(["addons\{$identify}\jobs", $job_method_name],$job_data);
    }
}
